'use strict';

app.controller('LogCtrl',
['$scope','$http', 'dialogs', '$state',
function($scope, $http, dialogs, $state){
	var vm = $scope.vm = this;
	var defaultQuery = {searchType:"account", moduleId:'',keyword:'',beginTime:null,endTime:null};
	vm.query = angular.copy(defaultQuery);
	$http.post(ISS.getPath('/log/getLogModules')).then(function(resp){
		var data = resp.data;
		vm.modules = data;
	});
	
	vm.flexigridObj = {};
	var _gridWidth = 1024;
	var _columnWidth = _gridWidth / 8;
	vm.options = {
		//height: getGridHeight(),
		url: ISS.getPath('/log/queryLog'),
		dataType: 'json',
		colModel : [
			{display: 'ID', name : 'id', hide:'true'},
			{display: 'log.operator', name : 'account', width : _columnWidth},
			{display: 'log.operationModule', name : 'modelName', width : _columnWidth},
			{display: 'log.operationContent', name : 'content', width : _columnWidth*2.84},
			{display: 'log.operationResult', name : 'status', width : _columnWidth,process:function(v){
				return v == 1 ? getMessage('common.success') : getMessage('common.fail');
			}},
            {
                display: 'log.operationTime',
                name: 'createTime',
                width: _columnWidth * 1.18,
                process: function (value) {
                    return moment(value).format("YYYY-MM-DD hh:mm:ss");
                }
            }
		],
		rp: 10,
		usepager: true
	};
	
	$scope.popup1 = {
	    opened: false
	};
	$scope.open1 = function() {
	   $scope.popup1.opened = true;
	};
	$scope.popup2 = {
	    opened: false
	};
	$scope.open2 = function() {
	   $scope.popup2.opened = true;
	};
	
	//查询
	vm.search = function(){
		if(vm.query.beginTime > vm.query.endTime){
			dialogs.notify(getMessage("error.timeRangeError"));
			return;
		}else {
			var param = angular.copy(vm.query);
			if(ISS.isNotEmpty(param.beginTime)){
				param.beginTime = moment(param.beginTime).format('yyyy-MM-dd') + ' 00:00:00';
			}
			if(ISS.isNotEmpty(param.endTime)){
				param.endTime = moment(param.endTime).format('yyyy-MM-dd') + ' 23:59:59';
			}
			
			param[param.searchType] = param.keyword;
			delete param.searchType;
			delete param.keyword;
			vm.flexigridObj.flexOptions({
				newp: 1,
				extParam: param
			}).flexReload();
		}
	}

	// 重置
	vm.reset = function(){
		vm.query = angular.copy(defaultQuery);
		vm.search();
	}
}]);